package proyectoFC_2_3_3.koyicabuto;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomCursorAdapterCheck extends android.widget.CursorAdapter{
	
	private LayoutInflater mLayoutInflater;
	private Context mContext;
	
	private ListaContactosActivity listaContactosActivity;
	
	public CustomCursorAdapterCheck(Context context, Cursor c, ListaContactosActivity listaContactosActivity) {
        super(context, c);
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        this.listaContactosActivity = listaContactosActivity;
    }
	
	@Override
	public void bindView(final View view, final Context context, final Cursor cursor) {		
		ViewHolder holder = (ViewHolder)view.getTag();
		holder.setTextViewNo((TextView)view.findViewById(R.id.Nombre));
		holder.setTextViewNu((TextView)view.findViewById(R.id.Numero));
		holder.setImage((ImageView)view.findViewById(R.id.Foto));
		holder.setCheckBox((CheckBox)view.findViewById(R.id.CheckBox));
		
		final CheckBox cb = holder.getCheckBox();
		cb.setTag(new Integer(cursor.getPosition()));
		
		holder.getTextViewNo().setText(cursor.getString(1));
		holder.getTextViewNu().setText(cursor.getString(2));
		Uri u = getPhotoUri(cursor.getLong(3));
		if (u != null) {
			holder.getImage().setImageURI(u);
			
			BitmapDrawable drawable = (BitmapDrawable)holder.getImage().getDrawable();
			Bitmap bitmap = drawable.getBitmap();
			holder.getImage().setImageBitmap(getRoundedCornerBitmap(bitmap, 18));
			
		} else {
			holder.getImage().setImageResource(R.drawable.silueta_tecnico);
		}
		
		cb.setOnClickListener(new OnClickListener() {	
			public void onClick(View v) {
				Integer currentPosition = (Integer)cb.getTag();
				if(cursor.moveToPosition(currentPosition)){
					listaContactosActivity.getCheckedItems(cursor.getLong(3));
				}
			}
		});
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		 View v = mLayoutInflater.inflate(R.layout.listacontactositemcheck, parent, false);
		 ViewHolder holder;
		 holder = new ViewHolder(v);
		 v.setTag(holder);
	     return v;
	}
	
	
	public Uri getPhotoUri(Long id) {
	    try {
	        Cursor cur = mContext.getContentResolver().query(
	                Data.CONTENT_URI,
	                null,
	                Data.CONTACT_ID + "=" + id + " AND "
	                        + ContactsContract.Data.MIMETYPE + "='"
	                        + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'", null,
	                null);
	        if (cur != null) {
	            if (!cur.moveToFirst()) {
	                return null; // no photo
	            }
	        } else {
	            return null; // error in cursor process
	        }
	    } catch (Exception e) {
	        e.printStackTrace();
	        return null;
	    }
	    Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
	    return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
	}
	
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels ) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }
	
	public class ViewHolder {
		
		private TextView textViewNo = null;
		private TextView textViewNu = null;
		private ImageView image = null;
		private CheckBox checkBox = null;
		private View base;
		
		public ViewHolder(View base){
			this.base = base;
		}

		public TextView getTextViewNo() {
			return textViewNo;
		}

		public void setTextViewNo(TextView textViewNo) {
			this.textViewNo = textViewNo;
		}

		public TextView getTextViewNu() {
			return textViewNu;
		}

		public void setTextViewNu(TextView textViewNu) {
			this.textViewNu = textViewNu;
		}

		public ImageView getImage() {
			return image;
		}

		public void setImage(ImageView image) {
			this.image = image;
		}

		public CheckBox getCheckBox() {
			return checkBox;
		}

		public void setCheckBox(CheckBox checkBox) {
			this.checkBox = checkBox;
		}

		public View getBase() {
			return base;
		}

		public void setBase(View base) {
			this.base = base;
		}
	}
}
